package com.example.spring;

import com.sun.deploy.util.StringUtils;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        class Student implements  Comparable<Student>{
            private int age;
            private String name;

            public Student(int age, String name) {
                this.age = age;
                this.name = name;
            }

            @Override
            public boolean equals(Object o) {
                if (this == o) return true;
                if (o == null || getClass() != o.getClass()) return false;
                Student student = (Student) o;
                return age == student.age && Objects.equals(name, student.name);
            }

            @Override
            public int hashCode() {
                return Objects.hash(age, name);
            }

            @Override
            public int compareTo(Student o) {
                return o.age-this.age;
            }

            @Override
            public String toString() {
                return "Student{" +
                        "age=" + age +
                        ", name='" + name + '\'' +
                        '}';
            }
        }
        List<Student> studentList =new LinkedList<>();
        studentList.add(new Student(18,"zhangsan1"));
        studentList.add(new Student(19,"zhangsan2"));
        studentList.add(new Student(11,"zhangsan3"));
        studentList.add(new Student(11,"zhangsan3"));
        studentList.add(new Student(182,"zhangsan4"));
        studentList.add(new Student(183,"zhangsan5"));
        studentList.add(new Student(184,"zhangsan6"));
        studentList.add(new Student(185,"zhangsan7"));
        System.out.println(studentList.size());
        Set<Student> studentSet = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.name.compareTo(o2.name);
            }
        });
        studentSet.addAll(studentList);
        System.out.println(studentSet.size());
        System.out.println(studentSet);
        Map<Integer,Student> map = new HashMap<>();
        for (Student student : studentList) {
            map.put(student.age,student);
        }
        Student student = map.get(18999);
        System.out.println(student);
        System.out.println(map.size());
        System.out.println(map);
        // 在你的代码末尾添加：
        System.out.println("\n===== HashMap 转 List =====");

// 1. 转换所有的 Key 到 List
        List<Integer> ageList = new ArrayList<>(map.keySet());
        System.out.println("Key List (年龄列表): " + ageList);

// 2. 转换所有的 Value 到 List
        List<Student> studentList1 = new ArrayList<>(map.values());
        System.out.println("Value List (学生列表): " + studentList);

// 3. 转换所有的 Entry 到 List
        List<Map.Entry<Integer, Student>> entryList = new ArrayList<>(map.entrySet());
        System.out.println("Entry List (键值对列表): " + entryList);

// 4. 遍历 EntryList（示例）
        for (Map.Entry<Integer, Student> entry : entryList) {
            System.out.println("年龄: " + entry.getKey() + ", 学生: " + entry.getValue());
        }

    }
}
